//主列表的IPC事件
const { ipcMain, app } = require("electron");
const { todaySync } = require('./task')
const fs = require('fs');
const path = require('path');
const { clearDataBase } = require('./dataBase')
const { clearStorage, getData } = require('./storage')

module.exports = async () => {
  //获取今日研究，可条件查询
  // ipcMain.on('STUDYLIST-QUERY', async (event, arg) => {
  //   //返回的研究列表
  //   const resList = []
  //   //获取path
  //   const directoryPath = await getData('filePath')
  //   if (directoryPath) {
  //     try {
  //       //解析参数
  //       const { date, name, age, gender, device } = arg
  //       let searchMilsStart = undefined
  //       let searchMilsEnd = undefined
  //       if (date) {
  //         searchMilsStart = new Date(`${date[0]} 00:00:00`).getTime()
  //         searchMilsEnd = new Date(`${date[1]} 23:59:59`).getTime()
  //       }
  //       const yearfiles = fs.readdirSync(directoryPath);
  //       yearfiles.sort((a, b) => Number(b) - Number(a))
  //       for (const yearName of yearfiles) {
  //         //年份文件夹
  //         //如果年份格式不对，扔出错误
  //         if (!(/^\d{4}$/.test(yearName))) {
  //           throw new Error('year pattern error')
  //         }
  //         //筛选掉年份
  //         if (date) {
  //           const yearMilsStart = new Date(`${yearName}-01-01 00:00:00`).getTime()
  //           const yearMilsEnd = new Date(`${yearName}-12-31 23:59:59`).getTime()
  //           if (searchMilsStart > yearMilsEnd || searchMilsEnd < yearMilsStart) continue
  //         }
  //         // console.log(yearName);
  //         const monthfiles = fs.readdirSync(path.join(directoryPath, yearName));
  //         monthfiles.sort((a, b) => Number(b) - Number(a))
  //         for (const monthName of monthfiles) {
  //           //月份文件夹
  //           //如果月份格式不对，扔出错误
  //           if (!(/^\d{2}$/.test(monthName))) {
  //             throw new Error('month pattern error')
  //           }
  //           //筛选掉月份
  //           if (date) {
  //             const monthMilsStart = new Date(`${yearName}-${monthName}-01 00:00:00`).getTime()
  //             const monthMilsEnd = new Date(`${yearName}-${Number(monthName) + 1}-01 00:00:00`).getTime()
  //             if (searchMilsStart >= monthMilsEnd || searchMilsEnd < monthMilsStart) continue
  //           }
  //           // console.log(monthName);
  //           const dayfiles = fs.readdirSync(path.join(directoryPath, yearName, monthName));
  //           dayfiles.sort((a, b) => Number(b) - Number(a))

  //           for (const dayName of dayfiles) {
  //             //日期文件夹
  //             //如果日期格式不对，扔出错误
  //             if (!(/^\d{2}$/.test(dayName))) {
  //               throw new Error('day pattern error')
  //             }
  //             //筛选掉日期
  //             if (date) {
  //               const dayMilsStart = new Date(`${yearName}-${monthName}-${dayName} 00:00:00`).getTime()
  //               const dayMilsEnd = new Date(`${yearName}-${monthName}-${dayName} 23:59:59`).getTime()
  //               if (searchMilsStart > dayMilsEnd || searchMilsEnd < dayMilsStart) continue
  //             }
  //             // console.log(dayName);
  //             const studyfiles = fs.readdirSync(path.join(directoryPath, yearName, monthName, dayName));
  //             //研究排序会损耗性能，暂时不开启
  //             // studyfiles.sort((a, b) => {
  //             //   const aPath = path.join(DirectoryPath, yearName, monthName, dayName, a)
  //             //   const aTime = getFileTime(aPath)
  //             //   const bPath = path.join(DirectoryPath, yearName, monthName, dayName, b)
  //             //   const bTime = getFileTime(bPath)
  //             //   return bTime - aTime
  //             // })
  //             for (const studyName of studyfiles) {
  //               //研究文件夹
  //               const dataStrs = studyName.split('-')
  //               //如果研究格式不对，扔出错误
  //               if (dataStrs.length < 5) {
  //                 throw new Error('studyName pattern error')
  //               }
  //               const id = encodeURIComponent(path.join(directoryPath, yearName, monthName, dayName, studyName))
  //               const study = {
  //                 id,
  //                 bh:dataStrs[0],
  //                 device: dataStrs[1],
  //                 age: dataStrs[2],
  //                 gender: dataStrs[3],
  //                 name: dataStrs[4],
  //                 date: `${yearName}-${monthName}-${dayName}`
  //               }
  //               //数据筛选2
  //               if (device && !device.includes(study.device)) continue
  //               if (age && age != study.age) continue
  //               if (gender && gender != study.gender) continue
  //               if (name && study.name.indexOf(name) == -1) continue
  //               let patient = await selectPatient(id)
  //               if (!patient) {
  //                 patient = await insertPatient(study)
  //               }
  //               resList.push(Object.assign(study, patient))
  //               console.log(study);
  //             }
  //           }
  //         }
  //       }
  //     } catch (error) {
  //       dialog.showErrorBox('错误', '读取资源失败，请检查配置。\n' + error)
  //     }
  //   }
  //   event.returnValue = { data: resList }
  // })

  ipcMain.on('CACHE-RESET', async (event, args) => {
    //清除所有缓存数据，清楚后退出软件
    await clearDataBase()
    clearStorage()
    app.exit()
  })

};

//获取文件夹的创建事件
const getFileTime = (folderPath) => {
  return new Date(fs.statSync(folderPath).birthtime.toLocaleString()).getTime()
}